package co.gov.medellin.ssti.sub.web.action;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import co.gov.medellin.ssti.sub.common.util.Utility;
import co.gov.medellin.ssti.sub.model.vo.CondicionConsultaVO;
import co.gov.medellin.ssti.sub.model.vo.RepAtributoVO;

public class TotalSelectFilterRGFAction extends Action {

	/**
	 * This is the main action called from the Struts framework.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 * @return
	 * @throws java.io.IOException
	 * @throws javax.servlet.ServletException
	 */
	@SuppressWarnings("unchecked")
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		HttpSession session = request.getSession();

		List<CondicionConsultaVO> lFilters = new LinkedList<CondicionConsultaVO>();
		List<RepAtributoVO> lShowFilters = new LinkedList<RepAtributoVO>();

		String strValue1 = null;
		String strValue2 = null;
		// String strFilter = null;
		String strCodeField = null;
		String strNomField = null;
		String strOrigin = null;

		Object objAccion = (Object) request.getParameter("accion");
		Object objAccion2 = (Object) request.getParameter("accion2");

		session.removeAttribute("ListReportGrupoFam");
		if (objAccion == null && objAccion2 == null) {
			session.removeAttribute("ListFiltrado");
			session.removeAttribute("CodeZona");
			session.removeAttribute("CodeComuna");
			session.removeAttribute("CodeBarrio");
			session.removeAttribute("CodePrograma");
			session.removeAttribute("CodeSubPrograma");
		}

		String strCodeZona = (String) request.getParameter("codeZona");
		String strCodeComuna = (String) request.getParameter("codeComuna");
		String strCodeBarrio = (String) request.getParameter("codeBarrio");
		String strCodePrograma = (String) request.getParameter("codePrograma");
		String strCodeSubPrograma = (String) request
				.getParameter("codeSubPrograma");

		RepAtributoVO objrepatriVO = null;
		CondicionConsultaVO objCondConsVO = null;

		String ArrAtri[] = { Utility.NAME_GRUPOFAM, Utility.NAME1_BENE,
				Utility.NAME2_BENE, Utility.APE1_BENE, Utility.APE2_BENE,
				Utility.NUMERO_DOCUMENTO, Utility.NUMERO_BENEFICIOS };
		String ArrAtri2[] = { Utility.NAME_GRUPOFAM_E, Utility.NAME1_BENE_E,
				Utility.NAME2_BENE_E, Utility.APE1_BENE_E, Utility.APE2_BENE_E,
				Utility.NUMERO_DOCUMENTO_E, Utility.NUMERO_BENEFICIOS_E };

		for (int j = 0; j < ArrAtri2.length; j++) {
			objrepatriVO = new RepAtributoVO();
			objrepatriVO.setNombre(ArrAtri[j]);
			objrepatriVO.setEtiqueta(ArrAtri2[j]);
			if (ArrAtri[j].equals(Utility.NUMERO_BENEFICIOS)) {
				objrepatriVO.setOrigen(Utility.ORIGEN_N);
			} else {
				objrepatriVO.setOrigen(Utility.ORIGEN_A);
			}
			lShowFilters.add(objrepatriVO);
		}

		if (objAccion != null) {
			if (objAccion.equals("Find")) {
				strCodeField = request.getParameter("CodeField");
				// strFilter = request.getParameter("CodeFilter");
				strValue1 = request.getParameter("CodeValue1");
				strValue2 = request.getParameter("CodeValue2");
				Object objFil = (Object) session.getAttribute("ListFiltrado");
				if (objFil != null) {
					lFilters = (List<CondicionConsultaVO>) session
							.getAttribute("ListFiltrado");
				}

				if (strCodeField.equals(Utility.NAME_GRUPOFAM)) {
					strNomField = Utility.NAME_GRUPOFAM_E;
					strValue1 = strValue1.toUpperCase();
					strOrigin = Utility.ORIGEN_A;
				} else if (strCodeField.equals(Utility.NAME1_BENE)) {
					strNomField = Utility.NAME1_BENE_E;
					strOrigin = Utility.ORIGEN_A;
				} else if (strCodeField.equals(Utility.NAME2_BENE)) {
					strNomField = Utility.NAME2_BENE_E;
					strOrigin = Utility.ORIGEN_A;
				} else if (strCodeField.equals(Utility.APE1_BENE)) {
					strNomField = Utility.APE1_BENE_E;
					strOrigin = Utility.ORIGEN_A;
				} else if (strCodeField.equals(Utility.APE2_BENE)) {
					strNomField = Utility.APE2_BENE_E;
					strOrigin = Utility.ORIGEN_A;
				} else if (strCodeField.equals(Utility.NUMERO_DOCUMENTO)) {
					strNomField = Utility.NUMERO_DOCUMENTO_E;
					strOrigin = Utility.ORIGEN_N;
				} else if (strCodeField.equals(Utility.NUMERO_BENEFICIOS)) {
					strNomField = Utility.NUMERO_BENEFICIOS_E;
					strOrigin = Utility.ORIGEN_N;
				}

				// Agregar filtro
				if (objAccion2.equals("Add")) {
					objCondConsVO = new CondicionConsultaVO();
					objCondConsVO.setCampo(strNomField);
					objCondConsVO.setCodcampo(strCodeField);
					objCondConsVO.setValor(strValue1);
					objCondConsVO.setValor2(strValue2);
					objCondConsVO.setOrigen(strOrigin);
					objCondConsVO.setCodigo(lFilters.size() + "");
					String strCodeFilter = request.getParameter("filter");
					int iFiltro = Integer.parseInt(strCodeFilter.trim());
					switch (iFiltro) {
					case 1:// filtro >=
						objCondConsVO.setFiltro(Utility.MAYOR_IGUAL_E);
						objCondConsVO.setCodFiltro(Utility.MAYOR_IGUAL);
						break;
					case 2:// filtro <=
						objCondConsVO.setFiltro(Utility.MENOR_IGUAL_E);
						objCondConsVO.setCodFiltro(Utility.MENOR_IGUAL);
						break;
					case 3:// filtro =
						objCondConsVO.setFiltro(Utility.IGUAL_E);
						objCondConsVO.setCodFiltro(Utility.IGUAL);
						break;
					case 4:// filtro <>
						objCondConsVO.setFiltro(Utility.DIFERENTE_E);
						objCondConsVO.setCodFiltro(Utility.DIFERENTE);
						break;
					case 5:// filtro <
						objCondConsVO.setFiltro(Utility.MENOR_E);
						objCondConsVO.setCodFiltro(Utility.MENOR);
						break;
					case 6:// filtro >
						objCondConsVO.setFiltro(Utility.MAYOR_E);
						objCondConsVO.setCodFiltro(Utility.MAYOR);
						break;
					case 7:// filtro BETWEEN
						objCondConsVO.setFiltro(Utility.ENTRE_E);
						objCondConsVO.setCodFiltro(Utility.ENTRE);
						break;
					case 8:// filtro LIKE
						objCondConsVO.setFiltro(Utility.PARECIDO_E);
						objCondConsVO.setCodFiltro(Utility.PARECIDO);
						break;
					}
					lFilters.add(objCondConsVO);
				}
			}
		}

		// eliminar un filtro
		String strCodigo = request.getParameter("codigos");
		if (strCodigo != null) {
			CondicionConsultaVO objCondiCons = new CondicionConsultaVO();
			lFilters = (List<CondicionConsultaVO>) session
					.getAttribute("ListFiltrado");
			for (int s = 0; s < lFilters.size(); s++) {
				objCondiCons = (CondicionConsultaVO) lFilters.get(s);
				if (objCondiCons.getCodigo().toString().equals(strCodigo)) {
					lFilters.remove(s);
				}
			}
		}
		// Fin del proceso

		if (lFilters.size() != 0) {
			session.setAttribute("ListFiltrado", lFilters);
		}

		request.setAttribute("CodeField", strCodeField);
		request.setAttribute("TypeData", strOrigin);
		request.setAttribute("ListShowFilters", lShowFilters);

		// Proceso para guardar los criterios de busqueda

		if (strCodeZona != null && !strCodeZona.equals("nulo"))
			session.setAttribute("CodeZona", strCodeZona);
		if (strCodeComuna != null && !strCodeComuna.equals("nulo"))
			session.setAttribute("CodeComuna", strCodeComuna);
		if (strCodeBarrio != null && !strCodeBarrio.equals("nulo"))
			session.setAttribute("CodeBarrio", strCodeBarrio);
		if (strCodePrograma != null && !strCodePrograma.equals("nulo"))
			session.setAttribute("CodePrograma", strCodePrograma);
		if (strCodeSubPrograma != null && !strCodeSubPrograma.equals("nulo"))
			session.setAttribute("CodeSubPrograma", strCodeSubPrograma);
		// Fin del proceso

		return mapping.findForward("success");
	}
}